package com.example.yckjbigdataflink.utils;

import java.util.ArrayList;
import java.util.List;

public class RowKeySplitter {

    /**
     * 简单示例：根据前缀分片
     * 实际生产应结合业务RowKey设计做更合理分片（数字区间、时间戳区间等）
     */
    public static List<RowKeyRange> splitByPrefix(int numShards) {
        List<RowKeyRange> shards = new ArrayList<>();
        char start = '0';
        char end = 'z';

        int totalRange = end - start + 1;
        int shardSize = totalRange / numShards;

        for (int i = 0; i < numShards; i++) {
            char shardStart = (char)(start + i * shardSize);
            char shardEnd = (i == numShards - 1) ? end : (char)(shardStart + shardSize - 1);

            String startKey = String.valueOf(shardStart);
            String endKey = String.valueOf(shardEnd) + "~"; // ~是ASCII较大字符，确保包含结束前缀

            shards.add(new RowKeyRange(startKey, endKey));
        }
        return shards;
    }

    public static class RowKeyRange {
        public String startRowKey;
        public String endRowKey;

        public RowKeyRange(String startRowKey, String endRowKey) {
            this.startRowKey = startRowKey;
            this.endRowKey = endRowKey;
        }

        @Override
        public String toString() {
            return "RowKeyRange{" + "start='" + startRowKey + '\'' + ", end='" + endRowKey + '\'' + '}';
        }
    }

    // 测试示例
    public static void main(String[] args) {
        List<RowKeyRange> ranges = splitByPrefix(5);
        for (RowKeyRange r : ranges) {
            System.out.println(r);
        }
    }
}
